﻿using System.Linq;
using PSE.Deployment.Engine.Interfaces;
using PSE.Deployment.SetupBase;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Groups;
using PSE.Deployment.Util;
using Sinacor.Deployment.Setup.Main;

namespace Sinacor.Deployment.Setup.Install
{
    public class SinacorRepair : SetupRepair
    {
        public SinacorRepair(IStepGroup parent)
            : base(parent)
        {
            this.Id = "SinacorRepair";
            this.Description = "Reparação do Sinacor";
        }

        protected override void AddPosStepsExecution()
        {
            AddUpdateVersionStep();

            AddLicenceStep();

            AddBoostStep();
       
            base.AddPosStepsExecution();
        }

        private void AddBoostStep()
        {
            SetupGroup setupGroup = new SetupGroup(this, new Group() { Id = "RegisterBoostGroup", Description = "Registro de referências" });

            setupGroup.Steps.Add(new Sinacor.Deployment.Setup.Install.Steps.RegisterBoostFile(setupGroup, setupGroup.Group,
                new PSE.Deployment.SetupBase.Configuration.Actions.Copy() { Id = "RegisterBoostFile", Description = "Registra arquivo boost do quickfix" }, SetupMainFactory.GetInstance().Parameter.SetupMode));

            this.Steps.Add(setupGroup);
        }

        private void AddUpdateVersionStep()
        {
            SetupGroup setupGroup = new SetupGroup(this, new Group() { Id = "UpdateVersion", Description = "Atualização das versões" });

            setupGroup.Steps.Add(new Sinacor.Deployment.Setup.Install.Steps.UpdateInterfaceLayerVersion(setupGroup));

            this.Steps.Add(setupGroup);
        }

        private void AddLicenceStep()
        {
            PSE.Deployment.SetupBase.Configuration.Group group = SetupMainFactory.GetInstance<SinacorMain>().Configuration.SystemInfo.Groups.SelectDeep(g => g.SubGroups).ToList().Find(g => g.Id == "ServiceLayer");

            if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.SetupMode == PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Install)
            {
                if (group != null && (group.Install == null || group.Install.GetValueOrDefault()))
                {
                    SetupGroup setupGroup = new SetupGroup(this, new Group() { Id = "LicenceGroup", Description = "Geração do arquivo de licença" });

                    setupGroup.Steps.Add(new Sinacor.Deployment.Setup.Install.Steps.GenerateLicenceFile(setupGroup));

                    this.Steps.Add(setupGroup);
                }
            }
            else if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.SetupMode == PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Uninstall)
            {
                if (group != null && (group.Install.HasValue && !group.Install.Value) && group.Installed)
                {
                    SetupGroup setupGroup = new SetupGroup(this, new Group() { Id = "LicenceGroup", Description = "Geração do arquivo de licença" });

                    setupGroup.Steps.Add(new Sinacor.Deployment.Setup.Uninstall.Steps.DeleteLicenceFile(setupGroup));

                    this.Steps.Add(setupGroup);
                }
            }
        }

        private void AddDataBaseGroup()
        {
            PSE.Deployment.SetupBase.Configuration.Group group = SetupMainFactory.GetInstance<SinacorMain>().Configuration.SystemInfo.Groups.SelectDeep(g => g.SubGroups).ToList().Find(g => g.Id == "Database");

            if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.SetupMode == PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Install)
            {
                if (group != null && (group.Install == null || group.Install.GetValueOrDefault()))
                {
                    this.Steps.Add(new Sinacor.Deployment.Setup.Install.DataBase.DataBaseGroup(this, group));
                }
            }
        }
    }
}